//
//  PeopleListViewController.m
//  iStage
//
//  Created by michael on 26/07/2008.
//  Copyright 2008 Legal Education Technology Lab. All rights reserved.
//

#import "PeopleListViewController.h"
#import "DataManager.h"
#import "EditPersonController.h"
#import <AddressBook/AddressBook.h>;

@implementation PeopleListViewController
@synthesize listType, personEditor;

- (id)initwithListType:(NSInteger)listTypeId {
	if (self = [super initWithNibName:@"PeopleListView" bundle:nil]) {
		listType = listTypeId;
		[self setListType:listTypeId];
		

	}
	return self;
}
- (id)initWithStyle:(UITableViewStyle)style {
	if (self = [super initWithStyle:style]) {
	}
	return self;
}

-(void)setListTypeTo:(NSInteger)listTypeId{ 
	listType = listTypeId;
	if (listTypeId == 0) {
		//cast list
		self.title =@"Cast List";
	}
	if (listTypeId == 1) {
		//crew list
		self.title =@"Crew List";
	}

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSMutableArray *dataSource;
	if (listType==0) {
		//cast list
		dataSource = [[DataManager sharedDataManager].currentProduction objectForKey:@"castlist"];
	}
	if (listType==1) {
		//crew list
		dataSource = [[DataManager sharedDataManager].currentProduction objectForKey:@"crewlist"];
	}
	return [dataSource count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *MyIdentifier = @"MyIdentifier";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
	}
	// Configure the cell
	//ABAddressBookRef addressbook= ABAddressBookCreate();
	NSMutableArray *dataSource ;
	if (listType==0) {
		//cast list
		dataSource = [[DataManager sharedDataManager].currentProduction objectForKey:@"castlist"];
	}
	if (listType==1)
	{
		dataSource = [[DataManager sharedDataManager].currentProduction objectForKey:@"crewlist"];
	}
	NSLog([[NSString alloc] initWithFormat:@"Datasource length %u",[dataSource count]]);
	NSLog([[NSString alloc] initWithFormat:@"Displaying Person at index %u",indexPath.row]);
	if (indexPath.row< [dataSource count]) {
		
		NSMutableDictionary *personInfo = [dataSource objectAtIndex:indexPath.row] ;
		if (personInfo != nil) {
			ABAddressBookRef addressbook = ABAddressBookCreate();
			NSString *displayText =@"unallocated";
			BOOL cast = NO;
			displayText= [personInfo objectForKey:@"role"];
			NSNumber *o =[personInfo objectForKey:@"actorabref"];
			NSLog([[NSString alloc] initWithFormat:@"Stored Actorref ID: %u", o]);
			if (o == nil | [o intValue] ==0) {
				NSLog(@"ActorRef is nill");
			}
			else {
				ABRecordRef actor = ABAddressBookGetPersonWithRecordID(addressbook, [o intValue]);
				NSString *actorName;
				if (actor == nil  ) {
					actorName = @"Not Cast";
				}
				else  {
					actorName = [[NSString alloc] initWithFormat:@"%@ %@", ABRecordCopyValue(actor, kABPersonFirstNameProperty),ABRecordCopyValue(actor, kABPersonLastNameProperty)];
					cast=YES;
				}
				displayText = [[NSString alloc] initWithFormat:@"%@ - %@",displayText,actorName];
			}
			cell.text=displayText;
			if (cast) {
				cell.accessoryType = UITableViewCellAccessoryCheckmark;
			}
			CFRelease(addressbook);
		}
		else {
			cell.text=@"Uknown state";
		}
	}
	//§	cell.text= [dataSource objectForKey:<#(id)aKey#>:indexPath.row];
	return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	NSLog(@"Displaying Person information");
	NSLog([[NSString alloc] initWithFormat:@"%u",listType]);
	if (indexPath.row>=0 & indexPath.row<=1) {
		if (indexPath.row ==0) {
			[[DataManager sharedDataManager]setCurrentPersonToCastAtIndex:indexPath.row];
			personEditor.ViewType = 0;
		}
		if (indexPath.row==1) {
			[[DataManager sharedDataManager]setCurrentPersonToCrewAtIndex:indexPath.row];	
			personEditor.ViewType = 1;
		}
		[self.navigationController pushViewController:personEditor animated:YES];
	}
}


/*
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
	
	if (editingStyle == UITableViewCellEditingStyleDelete) {
	}
	if (editingStyle == UITableViewCellEditingStyleInsert) {
	}
}
*/
/*
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/
/*
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/


- (void)dealloc {
	[super dealloc];
}


- (void)viewDidLoad {
	[super viewDidLoad];
	personEditor =[ [EditPersonController alloc] initWithNibName:@"PersonEditController" bundle:nil];
	
	self.navigationItem.rightBarButtonItem=
	[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
												  target:self action:@selector(addPersonToList)];
	
	self.navigationItem.leftBarButtonItem = 
	[[UIBarButtonItem alloc]  initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backToProductionAction)];
}


- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	[self.tableView reloadData];
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
	NSLog(@"People List view did appear");
}

- (void)viewWillDisappear:(BOOL)animated {
}

- (void)viewDidDisappear:(BOOL)animated {
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
}

#pragma mark Edit/Add Actions
-(IBAction)backToProductionAction {
	NSLog(@"returning to production view");
	[self.navigationController popViewControllerAnimated:YES];
	NSLog(@"view popped");
}
-(IBAction)addPersonToList {
	NSLog(@"Adding a new person to list");
	if (listType ==0) {
		personEditor.title=@"New Cast Member";
		[[DataManager sharedDataManager] setCurrentPersonToNewForList:0];
	}
	if (listType==1) {
		personEditor.title=@"New Crew Member";
		[[DataManager sharedDataManager] setCurrentPersonToNewForList:1];
	}
	[self.navigationController pushViewController:personEditor animated:YES];
}

@end

